package leetcode.code447;

import java.util.HashMap;
import java.util.Map;

public class Solution extends Solution447 {

	@Override
	public int numberOfBoomerangs(int[][] points) {
		int len = points.length;
		if (len < 3) {
			return 0;
		}
		Map<Integer, Map<Integer, Integer>> per = new HashMap<>();
		// Map<距离,Map<脚标,距离脚标距离个数>>
		for (int i = 0; i < len - 1; i++) {
			for (int j = i + 1; j < len; j++) {
				int x = points[i][0] - points[j][0];
				int y = points[i][1] - points[j][1];
				int distance = x * x + y * y;
				if (per.containsKey(distance)) {
					Map<Integer, Integer> per1 = per.get(distance);
					if (per1.containsKey(i)) {
						per1.put(i, per1.get(i) + 1);
					} else {
						per1.put(i, 1);
					}
					if (per1.containsKey(j)) {
						per1.put(j, per1.get(j) + 1);
					} else {
						per1.put(j, 1);
					}
				} else {
					Map<Integer, Integer> map = new HashMap<>();
					map.put(i, 1);
					map.put(j, 1);
					per.put(distance, map);
				}
			}
		}
		int ans = 0;
		for (Integer key : per.keySet()) {
			Map<Integer, Integer> map = per.get(key);
			for (Integer keyi : map.keySet()) {
				int num = map.get(keyi);
				ans += num * (num - 1);
			}
		}
		return ans;
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
